package com.lori.netshop.service.imp;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lori.netshop.mapper.TbItemDescMapper;
import com.lori.netshop.mapper.TbItemMapper;
import com.lori.netshop.pojo.TbItem;
import com.lori.netshop.pojo.TbItemDesc;
import com.lori.netshop.pojo.TbItemExample;
import com.lori.netshop.service.ItemService;
import com.lori.netshop.util.IDUtil;
import com.lori.netshop.util.NetshopResult;
@Service
public class ItemServiceimp implements ItemService {
	//依赖注入mapper
	@Autowired
	private TbItemMapper tbItemMapper;
	@Autowired
	private TbItemDescMapper tbItemDescMapper;
	@Override
	public TbItem findTbItem(long tid) {
		TbItem item = tbItemMapper.selectByPrimaryKey(tid);
		return item;
	}
	/**
	 * 功能：获得分页item
	 */
	@Override
	public Map pageTbItem(int page, int rows) {
		Map map = new HashMap();
		TbItemExample  tbItemExample = new TbItemExample();
		//分页
		PageHelper.startPage(page, rows);
		//查询
		List<TbItem> TbItemlist = tbItemMapper.selectByExample(tbItemExample);
		//获得分页信息
		PageInfo<TbItem> pageinfo = new PageInfo<TbItem>(TbItemlist);
		map.put("total", pageinfo.getTotal());
		map.put("rows", TbItemlist);
		System.out.println(pageinfo.getTotal());
		return map;
	}
	/**
	 * 功能：插入item数据和相关联的itemdesc数据
	 * 
	 */
	@Override
	public NetshopResult save(TbItem tbitem,String desc) {
		//判断tbitem是否存在
		NetshopResult result = new NetshopResult(null);
		if(tbitem != null){
			//用于存储数据和结果
			//获得唯一的id
			//补全信息
			tbitem.setStatus((byte)0);
			tbitem.setCreated(new Date());
			tbitem.setUpdated(new Date());
			//调用mapper
			int insert = tbItemMapper.insert(tbitem);
			//补全tbitemdesc的信息
			TbItemDesc tbitemdesc = new TbItemDesc();
			tbitemdesc.setItemId(tbitem.getId());
			tbitemdesc.setItemDesc(desc);
			tbitemdesc.setCreated(new Date());
			tbitemdesc.setUpdated(new Date());
			tbItemDescMapper.insert(tbitemdesc);
			//设置返回信息
			result.setMsg("数据保存成功");
			return result;
		}
		result.setStatus(404);
		result.setMsg("保存失败");
		return result;
	}

}
